package com.aigou.admin.aigouspringboot.controller.admin;

import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.ProductType;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.ProductTypeService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
/**
 * @Author Mr.LiFuXing
 * @Date 2024/12/10 20:22
 * @Title UserController
 * @Package com.aigou.admin.aigouspringboot.controller
 */

@RestController("adminProductType")
@RequestMapping("/admin/ProductType")
@Slf4j
public class ProductTypeController {

    @Resource
    private ProductTypeService productTypeService;

    /**
     * 分页查询
     *
     * @param current 当前页
     * @return info
     */
    @GetMapping
    @LogScope(title = "用户管理模块", content = "查看用户列表信息")
    public Result select(@RequestParam Integer current) {

        log.info("查询第{}页的用户信息", current);
        // 分页查询 都返回PageInfo(分页插件工具)
        PageInfo<ProductType> userPageInfo = productTypeService.selectProductTypeListByPage(current);
        return Result.success(userPageInfo);
    }

    @GetMapping("/{id}")
    @LogScope(title = "用户管理模块", content = "根据id查询用户信息")
    public Result detail(@PathVariable String id){

        ProductType user = productTypeService.selectById(id);
        return Result.success(user);
    }

    @DeleteMapping("/{id}")
    @LogScope(title = "用户管理模块", content = "根据id删除用户信息")
    public Result delete(@PathVariable String id){

        int i = productTypeService.deleteById(id);
        return i>0 ? Result.success() : Result.error();
    }



    /**
     * 编辑用户
     * @param  productType
     * @return r
     */
    @PutMapping
    @LogScope(title = "用户管理模块", content = "编辑用户信息")
    public Result edit(@RequestBody ProductType productType){

        int i = productTypeService.edit(productType);
        return i>0 ? Result.success() : Result.error();
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/save")
    @LogScope(title = "用户管理模块", content = "添加用户信息")
    public Result save(@RequestBody ProductType user){

        user.setId(RandomUtil.getRandomNumber());
        int i = productTypeService.save(user);
        return i>0 ? Result.success() : Result.error();
    }
}
